package com.s2sh.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.s2sh.po.Items;

public class ItemsDao {
	
//	@Autowired
//	private SessionFactory sessionFactory;
	
	public List<Items> queryById(Integer id){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		
		String Hql = "From Items where Items.itemsid =: id";
		Query query = session.createQuery(Hql);
		query.setInteger("id", id);
		List<Items> list = query.list();
		
		trans.commit();
		session.close();
		
		return list;
	
	}

	public List<Items> listItems() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		
		String Hql = "From Items ";
		Query query = session.createQuery(Hql);
		List<Items> list = query.list();
		
		trans.commit();
		session.close();
		
		return list;
		
	}

}
